package com.namecard.namecardapi.controller;

import com.namecard.namecardapi.entity.DTO.OrderDTO;
import com.namecard.namecardapi.entity.DTO.OrderInfoDTO;
import com.namecard.namecardapi.entity.DTO.SpecialOrderDTO;
import com.namecard.namecardapi.entity.vo.*;
import com.namecard.namecardapi.exception.BusinessException;
import com.namecard.namecardapi.service.OrderInfoService;
import com.namecard.namecardapi.service.SpecialOrderService;
import com.namecard.namecardapi.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zxb
 */
@RestController
@RequestMapping("order")
@Api(value = "订单数据处理接口", tags = "订单数据处理接口")
@Slf4j
public class OrderController {
    @Resource
    OrderInfoService orderInfoService;

    @Autowired
    SpecialOrderService specialOrderService;


    /**
     * 创建未支付订单
     */
    @PostMapping("createOrder")
    @ApiOperation("创建未支付订单")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<String> createOrder(@RequestBody OrderDTO orderDTO, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            String orderId = orderInfoService.createOrder(userId, orderDTO);
            return ResponseVO.getSuccessResponseVo(orderId);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }

    }

    /**
     * 积分支付直接调用此方法
     */
    @PostMapping("payOrder/complete")
    @ApiOperation("积分支付直接调用此方法")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO complete(@RequestBody CompleteVO completeVO, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            TokenUtil.getUserId(token);
            return orderInfoService.complete(completeVO);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 取消支付,订单关闭
     */
    @PostMapping("payOrder/cancelled")
    @ApiOperation("取消支付,订单关闭")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<String> cancelled(@RequestBody String orderId, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            TokenUtil.getUserId(token);
            Boolean order = orderInfoService.cancelled(orderId);
            if (!order){throw  new BusinessException("取消订单失败");}
            return ResponseVO.getSuccessResponseVo("取消订单成功");
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }

    }

    /**
     * 获取未支订单列表接口
     */
    @GetMapping("getNoPayOrderList")
    @ApiOperation("获取未支订单列表接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<List<OrderListFinalVO>> getNoPayOrderList(HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            List<OrderListFinalVO> noPayOrderList = orderInfoService.getNoPayOrderList(userId);
            return ResponseVO.getSuccessResponseVo(noPayOrderList);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 获取已支付订单列表接口
     */
    @GetMapping("getPayOrderList")
    @ApiOperation("获取已支付订单列表接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<List<OrderCardInfoVO>> getPayOrderList(HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            List<OrderCardInfoVO> orderCardInfoList = orderInfoService.getPayOrderList(userId);
            return ResponseVO.getSuccessResponseVo(orderCardInfoList);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 获取已支付订单详情接口
     */
    @GetMapping("getPayOrderDetailsList/{orderId}")
    @ApiOperation("获取已支付订单详情接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<OrderDetailsVO> getPayOrderDetailsList(@PathVariable String orderId, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            OrderDetailsVO OrderDetailsVO = orderInfoService.getPayOrderDetailsList(userId, orderId);
            return ResponseVO.getSuccessResponseVo(OrderDetailsVO);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw new BusinessException("获取未支订单列表失败，请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());
        }
    }

    /**
     * 保存卡片订单购物车接口
     */
    @PostMapping("saveOrderCardCar/{shopUserId}/{shopId}")
    @ApiOperation("保存卡片订单购物车接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<String> saveOrderCardCar(@PathVariable  String shopUserId,@PathVariable String shopId,HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            boolean flag = orderInfoService.saveOrderCardCar(shopUserId, shopId, userId);
            if (flag){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("添加购物车订单成功");
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw new BusinessException("添加购物车订单失败，请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());
        }
    }

    /**
     * 删除订单
     */
    @PostMapping("deleteOrder")
    @ApiOperation("删除订单")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<String> deleteOrder(@RequestBody OrderInfoDTO orderInfoDTO, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            boolean flag = orderInfoService.deleteOrder(orderInfoDTO.getOrderId(), userId);
            if (flag){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("删除订单成功");
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw new BusinessException("删除订单失败，请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());
        }
    }

    /**
     * 创建特殊数据订单
     */
    @PostMapping("createSpecialOrder")
    @ApiOperation("创建特殊数据订单")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<String> createSpecialOrderList(@RequestBody SpecialOrderDTO specialOrderDTO, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            String specialOrder = specialOrderService.createSpecialOrder(userId, specialOrderDTO);
            return ResponseVO.getSuccessResponseVo(specialOrder);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 获取特殊需求订单列表
     */
    @GetMapping("getSpecialOrderList")
    @ApiOperation("获取特殊需求订单列表")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<List<SpecialOrderVO>> getSpecialOrderList(HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            List<SpecialOrderVO> list = orderInfoService.getSpecialOrderList(userId);
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 删除特殊需求订单
     */
    @GetMapping("delSpecialOrder/{orderId}")
    @ApiOperation("删除特殊需求订单")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<String> deleteSpecialOrder(@PathVariable Long orderId, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            boolean delete = orderInfoService.deleteSpecialOrder(userId, orderId);
            if (!delete){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("特殊数据购买订单删除成功");
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw new BusinessException("特殊数据购买订单删除失败，请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());
        }
    }
}
